package com.wang.lotterysystem.controller.handler;

import com.wang.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.wang.lotterysystem.common.exception.ControllerException;
import com.wang.lotterysystem.common.exception.ServiceException;
import com.wang.lotterysystem.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public CommonResult<?> exceptionHandler(Exception e){
        log.error("内部系统异常:{}",e.getMessage());
        return CommonResult.error(e.getMessage(),
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode());
    }
    @ExceptionHandler(ControllerException.class)
    public CommonResult<?> controllerExceptionHandler(ControllerException e){
        log.error("控制层发生错误:{}",e.getMsg());
        return CommonResult.error(e.getMsg(),//错误信息返回具体错误
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode());//错误码返回统一错误码500
    }
    @ExceptionHandler(ServiceException.class)
    public CommonResult<?> serviceExceptionHandler(ServiceException e){
        log.error("业务逻辑层发生错误:{}",e.getMsg());
        return CommonResult.error(e.getMsg(),
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode());
    }
}
